import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useSearchStore = defineStore(
  'search',
  () => {
    const historyList = ref<string[]>([])

    const clearSearch = () => {
      historyList.value = []
    }

    const searchHistory = (value: string) => {
      const hisIndex = historyList.value.findIndex((item) => value == item)
      if (hisIndex != -1) {
        historyList.value.splice(hisIndex, 1)
      }
      historyList.value.unshift(value)
    }

    return {
      historyList,
      clearSearch,
      searchHistory,
    }
  },
  {
    persist: true,
  },
)
